﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;
using System.Reflection;

namespace ConsoleApplication_linq
{
    class Program
    {
        public static void paramsInfinis(params string[] s)
        {
            foreach (string var in s)
            {
                Console.WriteLine("param: {0}",var);
            }
        }

        static void Main(string[] args)
        {
            
            //Initialisation des paramètress
            List<Personne> personnes = new List<Personne>();
            personnes.Add(new Personne(1, "Abadou","Chiboudou"));
            personnes.Add(new Personne(2, "Cyrilus", "Magnus"));
            personnes.Add(new Personne(3, "Nick", "Burns"));
            personnes.Add(new Personne(4, "Villain", "Guillaume"));
            personnes.Add(new Personne(5, "Kirox", "Dragonix"));
            List<Capacites> capacites = new List<Capacites>() {
                                            new Capacites{Id = 1 ,Boire=true,Dormir=false,Mange=true},
                                            new Capacites{Id = 4, Boire=true,Dormir=true,Mange=true},
                                            new Capacites{Id = 5, Boire=true,Dormir=false, Mange = true}
            };
            int[] tabint = { 0, 2, 5, 8, 12, 18 };
            //Where syntaxe 1 => Syntaxe Linq
            Console.WriteLine("===Where 1");
            var result = from c in personnes
                         where c.Nom.Contains("r")
                         select c;
            foreach (Personne tmp in result)
            {
                Console.WriteLine(tmp.ToString());    
            }
            //Where syntaxe 2 => Expression Lambda
            Console.WriteLine("===Where 2");
            var resultat = personnes.Where((Personne i) => i.Nom.Contains("r"));
            foreach (Personne tmp in resultat)
            {
                Console.WriteLine(tmp.ToString());
            }
            //Instanciation d objet - C#3
            Console.WriteLine("===Initialisateur d'objets");
            Personne p = new Personne { Id = 1, Nom = "Guillaume", Prenom = "Test" };
            Console.WriteLine(p);
            //Type anonyme - C#3
            Console.WriteLine("===Type Anonyme");
            var lambda = new { Id = 1, Nom = "Guillaume", Liste = personnes };
            Console.WriteLine(lambda.Id + " " + lambda.Nom);
            foreach (Personne var in lambda.Liste)
            {
                Console.WriteLine(var.ToString());
            }
            //Jointure
            Console.WriteLine("===Jointure");
            var jointure = from f in personnes
                           join w in result
                           on f.Id equals w.Id
                           select f;
            foreach (Personne var in jointure)
            {
                Console.WriteLine(var.ToString());
            }
            //Sum, Max , Min, Count
            Console.WriteLine("===Sum,Max,Min,Count");
            Console.WriteLine("Sum : {0}", tabint.Sum());
            Console.WriteLine("Max : {0}", tabint.Max());
            Console.WriteLine("Min : {0}", tabint.Min());
            Console.WriteLine("Count : {0}",tabint.Count());
            //Linq To Sql
            Console.WriteLine("===Linq To SQL");
            DataClasses1DataContext context = new DataClasses1DataContext();
            context.Log = Console.Out;
            Table<Personnes> table_personne = context.GetTable<Personnes>();
            var in_db = from e in table_personne
                        select e;
            foreach (Personnes var in in_db)
            {
                Console.WriteLine(var.ToString());
            }
            var res = from r in table_personne
                         where r.nom.Contains("Guillaume")
                         select r;
            Console.WriteLine("===First");
            Console.WriteLine(res.First().ToString());
            //Ajout Base de donnees en linq
            Personnes newper = new Personnes { nom = "NewPrenom", prenom="NewPrenom"};
            context.Personnes.Add(newper);
            context.SubmitChanges();
            var last = context.Personnes.ToList();
            Console.WriteLine("Derniere element ajouté :  {0}",last.Last().id);
            //Méthodes d'extensions
            Console.WriteLine("===Méthodes d'extension");
            string s = "  hello world! ";
            Console.WriteLine("Normal : |{0}| == Appel Fonction : |{1}|",s,s.Transforme());
            //Params infinsi a une fonction
            Console.WriteLine("===Parametres infinis a une fonction : varargs en C");
            Program.paramsInfinis("ca va ", "tu vas bien", "oui bien sur");
            //Reflexion
            Assembly ass = Assembly.LoadFrom("ClassLibrary.dll");
            var types = ass.GetTypes().ToList();
            foreach (Type ti in types)
            {
                if (ti.IsClass)
                {
                    object test = Activator.CreateInstance(ti, new object[] { 1 });
                }
                Console.WriteLine(ti.FullName);
            }
            Console.ReadLine();
        }

        private static void Where(object p)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
